<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of category_model
 *
 * @author Hoa
 */
class category_model extends CI_Model {

    //put your code here
    var $category_name = '';
    var $category_name_segment = '';
    var $cagegory_tag = '';
    var $category_date = '';
    var $cagegory_order = 0;
    var $category_display_state = 0;
    var $category_is_delete = 0;
    var $category_parent_id = -1;

    function __construct() {
        parent::__construct();
    }

    function getAllCategories() {
        $this->db->select('category_id,category_name,category_order');
        $this->db->from(tbl_category);
        $arr = array('category_display_state' => 1, 'category_is_delete' => 0);
        $this->db->where($arr);
        $this->db->order_by('category_order');
        $query = $this->db->get();
        return $query->result();
    }

    function getCategories($page = 0, $page_size = PAGE_SIZE) {


        $this->db->limit($page_size, $page);
        $this->db->select('category_id,category_name,category_date,category_order,category_display_state');
        $this->db->from(tbl_category);
        $this->db->where('category_is_delete', 0);
        $this->db->order_by('category_order');
        $query = $this->db->get();


        //Get total rows
        $this->db->select('category_id');
        $this->db->from(tbl_category);
        $this->db->where('category_is_delete', 0);
        $total = $this->db->count_all_results();

        $data = array('list' => $query->result(),
            'total' => $total);
        return $data;
    }

    function getCategoryByID($id) {
        $query = $this->db->get_where(tbl_category, array('category_id' => $id));
        $arr = $query->result();
        $count = $query->num_rows();
        if ($count <= 0)
            return null;
        return $arr[0];
    }

    function checkExist($categoryName) {
        $this->db->select('category_name');
        $this->db->where('category_name like', $categoryName);
        $this->db->from(tbl_category);
        $query = $this->db->get();
        return $query->num_rows();
    }

    function addCategory($info) {

        return $this->db->insert(tbl_category, $info);
    }

    function updateCategory($info, $id) {
        $this->db->where('category_id', $id);
        return $this->db->update(tbl_category, $info);
    }

    function deleteCategory($id) {
        $this->db->where('category_id', $id);
        return$this->db->update(tbl_category, array('category_is_delete'=>1));
    }

    //Accessory Categories

    function getAccessoryCategories($page = 0, $page_size = PAGE_SIZE) {


        $this->db->limit($page_size, $page);
        $this->db->select('category_id,category_name');
        $this->db->from(tbl_accessory_category);
        $query = $this->db->get();


        //Get total rows
        $this->db->select('category_id');
        $this->db->from(tbl_category);
        $total = $this->db->count_all_results();

        $data = array('list' => $query->result(),
            'total' => $total);
        return $data;
    }

    function addAccessoryCategory($info) {

        return $this->db->insert(tbl_accessory_category, $info);
    }

    function updateAccessoryCategory($info, $id) {
        $this->db->where('category_id', $id);
        return $this->db->update(tbl_accessory_category, $info);
    }

    function deleteAccessoryCategory($id) {
        $this->db->where('category_id', $id);
        return $this->db->delete(tbl_accessory_category);
    }

    function getAllAccessoryCategories() {
        $query = $this->db->get(tbl_accessory_category);
        return $query->result();
    }

    function getAccessoryCategoryByID($id) {
        $query = $this->db->get_where(tbl_accessory_category, array('category_id' => $id));

        $arr = $query->result();
        $count = $query->num_rows();
        if ($count <= 0)
            return null;

        return $arr[0];
    }

}

